[LegacyColorValue = true]; 

{ TSM Moving Averages (2) : 2 Moving average system
  Copyright 1994-1999,2011 P J Kaufman. All rights reserved. }

  input: slowper(20), fastper(5), investment(1000000), volper(20);
  vars:	slowMA(0), fastMA(0), size(0), equity(0), atrange(0), allocation(0.05),
  			entrysize(0),
  			strategy(1);

	if currentbar = 1 then equity = investment;
	
	slowMA = average(close,slowper);
	fastMA = average(close,fastper);
 
	if volper = 0 then 
			size = 1
		else begin
 			atrange = avgtruerange(volper)*bigpointvalue;
			size = equity*allocation/atrange;
		end;
 	
  	if marketposition <> 1 and fastMA > slowMA then begin
  			if marketposition < 0 then buy to cover all contracts this bar on close;
  			Buy size contracts This Bar at close;
  			entrysize = size;
  			end
  		else if marketposition <> -1 and fastMA < slowMA then begin
  			if marketposition > 0 then sell all contracts this bar on close;
  			Sell Short size contracts This Bar on close;
  			entrysize = size;
 		end;
	
  	if strategy = 0 then equity = equity + marketposition*entrysize*(Close - close[1])*bigpointvalue;
  
	if currentbar = 1 then print(file("c:\TSM5\MA(2)_var_size.csv"),
				"Date,Close,FastMA,SlowMA,NetPL,Vol,Equity,Position");
	
	print(file("c:\TSM5\MA(2)_var_size.csv"),date:8:0, ",", close:8:4, ",", fastMA:8:4, ",", slowMA:8:4, ",",
				netprofit+openpositionprofit:8:2, ",", atrange:8:2, ",", equity:8:2, ",", marketposition*size:5:3);